package com.lc.handlers;

import com.lc.enums.EnumSex;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author liuc
 * @date: 2020/5/22
 * @desc:
 */
public class EnumSexHandler extends BaseTypeHandler<EnumSex> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, EnumSex parameter, JdbcType jdbcType) throws SQLException {
        System.out.println("////////// EnumSexHandler setNonNullParameter come in ");
        ps.setString(i,parameter.name());
    }

    @Override
    public EnumSex getNullableResult(ResultSet rs, String columnName) throws SQLException {
        //通过BaseTypeHandler中的getResult方法调用
        System.out.println("////////// EnumSexHandler getNullableResult(columnName) come in ");
        return EnumSex.byName(rs.getString(columnName));
    }

    @Override
    public EnumSex getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        System.out.println("////////// EnumSexHandler getNullableResult(columnIndex) come in ");
        return EnumSex.byName(rs.getString(columnIndex));
    }

    @Override
    public EnumSex getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        System.out.println("////////// EnumSexHandler getNullableResult(CallableStatement) come in ");
        return EnumSex.byName(cs.getString(columnIndex));
    }
}
